<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2007/01/05 11:44:28 $)
// $RCSfile: winapp_fetchvisitors.php,v $ : $Revision: 1.19 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./modules/livesupport/functions_livesupport.php");
require_once ("./includes/functions_xml.php");
$xml = new XML;

$template->loadLanguageSection("livesupport");

$xml->addParentTag("swiftwinapp");

/**
* ###############################################
* Require the shared listing file used by both the winapp and the monitor
* ###############################################
*/
require_once ("./modules/livesupport/global_listing.php");
$viscount = 0;
foreach ($sessiondata as $key=>$val)
{
	$viscount++;
	$sessionid = $key;
	$visdata = array();
	$totalfootprints = count($footprints[$sessionid]);

	$xml->addParentTag("visitor");
		if (!empty($val["campaignid"]))
		{
			$xml->addTag("info", "<font color='#277DC9'>".$val["campaigntitle"]."</font>", array("title" => $_SWIFT["language"]["adcampaign"]));
		}

		$xml->addTag("id", $viscount);
		$xml->addTag("sessionid", $val["sessionid"]);

		if (in_array($val["sessionid"], $pendingchatvisitorsessionlist))
		{
			$visdata["incomingchat"] = "true";
			$visdata["inchat"] = "false";
			$visdata["state"] = $_SWIFT["language"]["incomingchat"];
			$visdata["chatterid"] = $pendingvisitordata[$val["sessionid"]]["userfullname"];
			$visdata["chatsessionid"] = $pendingvisitordata[$val["sessionid"]]["chatsessionid"];
			$visdata["department"] = $pendingvisitordata[$val["sessionid"]]["departmenttitle"];
		} else if (in_array($val["sessionid"], $inchatvisitorsessionlist)) {
			$visdata["incomingchat"] = "false";
			$visdata["inchat"] = "true";
			$visdata["state"] = $_SWIFT["language"]["inchat"];
			$visdata["chatterid"] = $inchatvisitordata[$val["sessionid"]]["userfullname"];
			$visdata["chatsessionid"] = $inchatvisitordata[$val["sessionid"]]["chatsessionid"];
			$visdata["department"] = $inchatvisitordata[$val["sessionid"]]["departmenttitle"];
			$visdata["staffname"] = $inchatvisitordata[$val["sessionid"]]["staffname"];
		} else {
			$visdata["incomingchat"] = "false";
			$visdata["inchat"] = "false";
			$visdata["state"] = $_SWIFT["language"]["browsing"];
			$visdata["chatterid"] = $val["ipaddress"];
			$visdata["chatsessionid"] = "NULL";
			$visdata["department"] = "NULL";
		}

		$xml->addTag("incomingchat", $visdata["incomingchat"]);
		$xml->addTag("inchat", $visdata["inchat"]);
		$xml->addTag("department", $visdata["department"]);
		$xml->addTag("chatstaffname", $visdata["staffname"]);
		$xml->addTag("waittime", "NULL");

		$xml->addTag("lastvisit", "false");
		$xml->addTag("lastchat", "false");
		$xml->addTag("state", $visdata["state"]);

		$xml->addTag("currentpage", $val["currentpage"]);
		$xml->addTag("pagetitle", $val["pagetitle"]);
		$xml->addTag("referrer", $val["referrer"]);
		$xml->addTag("noofpages", $totalfootprints);

		$xml->addTag("chatterid", $visdata["chatterid"]);
		$xml->addTag("ipaddress", $val["ipaddress"]);
		$xml->addTag("hostname", $val["hostname"]);
		$xml->addTag("country", trim($val["countryname"]));
		$xml->addTag("countrycode", $val["countrycode"]);
		$xml->addTag("browser", $val["browsername"]);
		$xml->addTag("browserversion", $val["browserversion"]);
		$xml->addTag("browsercode", $val["browsercode"]);
		$xml->addTag("resolution", $val["resolution"]);
		$xml->addTag("platform", $val["platform"]);
		$xml->addTag("operatingsys", $val["operatingsys"]);
		$xml->addTag("searchenginecode", $val["searchenginename"]);
		$xml->addTag("searchstring", $val["searchstring"]);
		$xml->addTag("min", $val["min"]);
		$xml->addTag("max", $val["max"]);
		$xml->addTag("diff", $val["max"]-$val["min"]);
		if (trim($val["rowbgcolor"]) != "") {
			$rgb = hex2rgb($val["rowbgcolor"]);
			$xml->addTag("rowbgcolor", $rgb["r"].",".$rgb["g"].",".$rgb["b"]);
		} else {
			$xml->addTag("rowbgcolor", $val["rowbgcolor"]);
		}
		if (trim($val["rowfrcolor"]) != "") {
			$rgb = hex2rgb($val["rowfrcolor"]);
			$xml->addTag("rowfrcolor", $rgb["r"].",".$rgb["g"].",".$rgb["b"]);
		} else {
			$xml->addTag("rowfrcolor", $val["rowfrcolor"]);
		}
		$xml->addTag("hasnotes", "false");
		$xml->addTag("isspider", "false");
		$xml->addTag("appversion", $val["appversion"]);
		$xml->addTag("colordepth", $val["colordepth"]);

		// We add this again at end because the XML Pointer in winapp will have to be resetted
		$xml->addTag("chatsessionid", $visdata["chatsessionid"]);
		$xml->addTag("hasnote", $val["hasnote"]);
		$xml->addTag("hascampaign", iif(!empty($val["campaignid"]), "1", "0"));

		$fpid = 0;
		for ($ii=0; $ii<count($footprints[$sessionid]); $ii++) {
			$fpid++;
			$xml->addParentTag("footprints");
			$xml->addTag("id", $fpid);
			$xml->addTag("pageurl", $footprints[$sessionid][$ii]["pageurl"]);
			$xml->addTag("pagetitle", iif(!trim($footprints[$sessionid][$ii]["pagetitle"]), "None", $footprints[$sessionid][$ii]["pagetitle"]));
			$xml->addTag("referrer", $footprints[$sessionid][$ii]["referrer"]);
			$xml->addTag("lastactivity", $footprints[$sessionid][$ii]["lastactivity"]);
			$xml->addTag("dateline", $footprints[$sessionid][$ii]["dateline"]);
			$xml->endTag("footprints");
		}
	$xml->endTag("visitor");
}

for ($ii=0; $ii<count($staff); $ii++)
{
	$xml->addParentTag("staff");
		$xml->addTag("staffname", $staff[$ii]["staffname"]);
		$xml->addTag("staffusername", $staff[$ii]["staffusername"]);
		$xml->addTag("staffid", $staff[$ii]["staffid"]);
		$xml->addTag("isonline", $staff[$ii]["isonline"]);
		$xml->addTag("onlinetime", $staff[$ii]["dateline"]);
		// We only send the session id for the staff that is online at the moment, sending sessionids of other staff can pose a security risk.
		if ($staff[$ii]["staffid"] == $_SWIFT["staff"]["staffid"])
		{
			$xml->addTag("sessionid", $staff[$ii]["sessionid"]);
		} else {
			$xml->addTag("sessionid", "");
		}
		$xml->addTag("status", $staff[$ii]["status"]);
	$xml->endTag("staff");
}


// ======= PENDING CHAT LIST =======
for ($ii=0; $ii<count($pendingchatlist); $ii++)
{
	$xml->addParentTag("pendingchat");
		$xml->addTag("chatid", $pendingchatlist[$ii]["chatid"]);
		$xml->addTag("displayname", $pendingchatlist[$ii]["displayname"]);
		$xml->addTag("chatsessionid", $pendingchatlist[$ii]["chatsessionid"]);
		$xml->addTag("visitorsessionid", $pendingchatlist[$ii]["visitorsessionid"]);
	$xml->endTag("pendingchat");	
}


// ======= TRANSFER TREE LIST =======
for ($ii=0; $ii<count($transfertreelist); $ii++)
{
	$xml->addParentTag("transferred");
		$xml->addTag("chatsessionid", $transfertreelist[$ii]["chatsessionid"]);
		$xml->addTag("visitorsessionid", $transfertreelist[$ii]["visitorsessionid"]);
		$xml->addTag("displayname", $transfertreelist[$ii]["displayname"]);
	$xml->endTag("transferred");
}

// ======= ALERTS =======
$xml->addParentTag("alerts");
	$xml->addTag("ticketalert", "");
	$xml->addTag("customalert", "");
$xml->endTag("alerts");

// ======= TRANSFERRED CHAT INITIATION =======
$chatexecqueue = array_merge($transferqueue, $proactivequeue, $staffchatqueue);
for ($ii=0; $ii<count($chatexecqueue); $ii++)
{
	$xml->addParentTag("chatexec");
		$xml->addTag("chatsessionid", $chatexecqueue[$ii]["chatsessionid"]);
		$xml->addTag("visitorsessionid", $chatexecqueue[$ii]["visitorsessionid"]);
		$xml->addTag("caption", $chatexecqueue[$ii]["caption"]);
		$xml->addTag("type", $chatexecqueue[$ii]["type"]);
		$xml->addTag("staffid", "0");
	$xml->endTag("chatexec");
}

// ======= INFO =======
$xml->addParentTag("info");
	$xml->addTag("companyname", $_SWIFT["settings"]["general_companyname"]);
	$xml->addTag("pendingchats", $pendingchatcount);
	$xml->addTag("activevisitors", $id);
	$xml->addTag("messagecount", getMessageCount());
	$xml->addTag("product", SWIFTPRODUCT);
$xml->endTag("info");

$xml->endTag("swiftwinapp");
/*
$fp = fopen("./cache/test.txt", "w+");
fwrite($fp, $xml->returnXML());
fclose($fp);*/

//$xml->echoXML();

$xml->echoXMLUTFWinapp();

// ======= UPDATE TRANSFER CHAT STATUS =======
if (count($transferlist) > 0)
{
	$dbCore->shutdownQuery("UPDATE `". TABLE_PREFIX ."chatobjects` SET `transferstatus` = '". TRANSFER_ACCEPTED ."' WHERE `chatobjectid` IN (". buildIN($transferlist) .");");
}

// ======= UPDATE PROACTIVE CHAT STATUS =======
if (count($proactivelist) > 0)
{
	$dbCore->shutdownQuery("UPDATE `". TABLE_PREFIX ."chatobjects` SET `isproactive` = '". PROACTIVE_NONE ."' WHERE `chatobjectid` IN (". buildIN($proactivelist) .");");	
}
?>